package com.sctv.news.sjd.ui.adapter.holder;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.util.Pair;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.gridsum.tracker.GridsumWebDissector;
import com.ruihang.generalibrary.ui.util.ViewUtils;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.DateUtils;
import com.sctv.news.sjd.R;
import com.sctv.news.sjd.beans.NewsItem;
import com.sctv.news.sjd.ui.utils.GlideUtil;
import com.sctv.news.sjd.ui.utils.ReadNewsHelper;
import com.sctv.news.sjd.ui.utils.SkipUtil;
import com.sctv.news.sjd.utils.TimeUtils;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 24小时新闻条目
 * Created by Administrator on 2017/7/5.
 */

public class News24Holder extends BaseAbsHolder<NewsItem> implements View.OnClickListener,ReadNewsHelper.OnNewsReadListener {
    @BindView(R.id.normal_layout)
    protected ConstraintLayout normalLayout;
    @BindView(R.id.item_news_dot)
    protected View dot;
    @BindView(R.id.item_news_line_top)
    protected View topLine;
    @BindView(R.id.item_news_line_bottom)
    protected View bottomLine;
    @BindView(R.id.item_news_divider)
    protected View divider;
    @BindView(R.id.item_news_image)
    protected CustomEXImageView img;
    @BindView(R.id.item_news_title)
    protected CustomFontTextView title;
    @BindView(R.id.item_news_24_time)
    protected CustomFontTextView time;
    @BindView(R.id.item_news_date)
    protected CustomFontTextView date;
    @BindView(R.id.item_news_mode_tag)
    protected CustomFontTextView liveTag;
    @BindView(R.id.item_news_tag)
    protected CustomFontTextView tag;
    @BindView(R.id.item_news_24_length)
    protected CustomFontTextView length;
    @BindView(R.id.item_news_24_media)
    protected CustomFontTextView media;

    @BindView(R.id.item_big_news)
    protected LinearLayout itemBigLayout;
    @BindView(R.id.item_news_big_dot)
    protected View bigDot;
    @BindView(R.id.item_news_line_big_top)
    protected View bigTopLine;
    @BindView(R.id.item_news_line_big_bottom)
    protected View bigBottomLine;
    @BindView(R.id.item_news_big_image)
    protected CustomEXImageView bigImg;
    @BindView(R.id.item_news_big_title)
    protected CustomFontTextView bigNewsTitle;
    @BindView(R.id.item_news_big_date)
    protected CustomFontTextView bigNewsDate;
    @BindView(R.id.item_news_24_big_media)
    protected CustomFontTextView bigMedia;
    @BindView(R.id.item_news_mode_big_tag)
    protected CustomFontTextView liveBigTag;
    @BindView(R.id.item_news_24_big_length)
    protected CustomFontTextView bigLength;
    @BindView(R.id.item_news_big_divider)
    protected View bigDivider;

    @BindView(R.id.news_list_title)
    protected RelativeLayout newsListTitle;

    private NewsItem temp;

    public News24Holder(Context context, View itemView) {
        super(context, itemView);
        itemView.setOnClickListener(this);
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(NewsItem t) {
        temp = t;
        if (t.getNewsId().equalsIgnoreCase("-1" )) {
            newsListTitle.setVisibility(View.GONE);
            normalLayout.setVisibility(View.GONE);
            itemBigLayout.setVisibility(View.GONE);
        } else {
            newsListTitle.setVisibility(View.GONE);
//            temp.setNewsMediaLength("00:02:12");
            if (temp.getIsBigPic() == 1) {
                //大图显示
                normalLayout.setVisibility(View.GONE);
                itemBigLayout.setVisibility(View.VISIBLE);
                GlideUtil.loadImgSmall169Def(context, t.getDataImg(), bigImg);
                bigNewsTitle.setText(t.getDataTitle());
                bigNewsDate.setText(DateUtils.formatDate(DateUtils.getDataDate(t), ""));

                if (t.isLiving()) {
                    ViewUtils.setViewsGone(bigLength, bigMedia);
                    liveBigTag.setVisibility(View.VISIBLE);
                } else {
                    liveBigTag.setVisibility(View.GONE);
                    if (t.getDataType() > 1) {
                        ViewUtils.setViewVisible(bigMedia);
                        if (TextUtils.isEmpty(t.getDataMediaLength())) {
                            bigLength.setVisibility(View.GONE);
                        } else {
                            String len = TimeUtils.formatTimeToHM(t.getDataMediaLength());
                            if (!TextUtils.isEmpty(len)) {
                                bigLength.setVisibility(View.VISIBLE);
                                bigLength.setText(len);
                            } else {
                                bigLength.setVisibility(View.GONE);
                            }
                        }
                    } else {
                        ViewUtils.setViewsGone(bigLength, bigMedia);
                    }
                }
                if (t.getAfterTitleMode() > 0) {
                    if (t.getAfterTitleMode() == 1) {
                        ViewUtils.setViewsGone(bigDivider);
                        bigTopLine.setVisibility(View.INVISIBLE);
                    } else if (t.getAfterTitleMode() == 2) {
                        ViewUtils.setViewsGone(bigDivider);
                    }
                } else {
                    ViewUtils.setViewsVisible(bigDot, bigDivider, bigTopLine);
                }
                if (t.getNewsType() == 2) { //音频
                    bigMedia.setText("音频");
                    bigMedia.setVisibility(View.VISIBLE);
                } else if (t.getNewsType() == 3) {   //视频
                    bigMedia.setText("视频");
                    bigMedia.setVisibility(View.VISIBLE);
                } else {
                    bigMedia.setVisibility(View.GONE);
                }
            } else {
                normalLayout.setVisibility(View.VISIBLE);
                itemBigLayout.setVisibility(View.GONE);
                if (t.getItemMode() > 0) {
                    dot.setVisibility(View.GONE);
                    if (t.getItemMode() == 1) {
                        topLine.setVisibility(View.GONE);
                    } else {
                        topLine.setVisibility(View.VISIBLE);
                    }
                    divider.setVisibility(View.GONE);
                } else {
                    if (dot.getVisibility() != View.VISIBLE) {
                        dot.setVisibility(View.VISIBLE);
                        topLine.setVisibility(View.VISIBLE);
                        divider.setVisibility(View.VISIBLE);
                    }
                }
                if (t.isLiving()) {
                    ViewUtils.setViewsGone(length, media);
                    liveTag.setVisibility(View.VISIBLE);
                } else {
                    liveTag.setVisibility(View.GONE);
                    if (t.getDataType() > 1) {
                        ViewUtils.setViewVisible(media);
                        if (TextUtils.isEmpty(t.getDataMediaLength())) {
                            length.setVisibility(View.GONE);
                        } else {
                            length.setVisibility(View.VISIBLE);
                            length.setText(TimeUtils.formatTimeToHM(t.getDataMediaLength()));
                        }
                    } else {
                        ViewUtils.setViewsGone(length, media);
                    }
                }
                if (t.getAfterTitleMode() > 0) {
                    if (t.getAfterTitleMode() == 1) {
                        ViewUtils.setViewsGone(dot, divider, topLine);
                    } else if (t.getAfterTitleMode() == 2) {
                        ViewUtils.setViewsGone(dot, divider);
                    }
                } else {
                    ViewUtils.setViewsVisible(dot, divider, topLine);
                }
                String timeStr = DateUtils.getDateHourString(DateUtils.getDataDate(t));
                time.setText(timeStr);
                date.setText(DateUtils.formatDate(DateUtils.getDataDate(t), ""));
                title.setText(t.getDataTitle());
                if (!TextUtils.isEmpty(t.getDataTag())) {
                    tag.setText(t.getDataTag());
                }
                GlideUtil.loadImgSmall169Def(context, t.getDataImg(), img);
                if (t.getNewsType() == 2) { //音频
                    media.setText("音频");
                    media.setVisibility(View.VISIBLE);
                } else if (t.getNewsType() == 3) {   //视频
                    media.setText("视频");
                    media.setVisibility(View.VISIBLE);
                } else {
                    media.setVisibility(View.GONE);
                }
            }
            //区县，隐藏时间线
            topLine.setVisibility(View.GONE);
            dot.setVisibility(View.GONE);
            bottomLine.setVisibility(View.GONE);
            bigTopLine.setVisibility(View.GONE);
            bigBottomLine.setVisibility(View.GONE);
            bigDot.setVisibility(View.GONE);
        }

        refreshHolder();

    }

    @Override
    public void setItemMode(int mode) {
        super.setItemMode(mode);
        if (mode == ITEMMODE_LAST) {
            bottomLine.setVisibility(View.GONE);
        } else {
//            bottomLine.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onDestory() {
        super.onDestory();
        if (itemView != null) {
            itemView.setOnClickListener(null);
        }
    }

    @Override
    public void onClick(View v) {
        if (temp == null) {
            Toast.makeText(context, "数据错误", Toast.LENGTH_SHORT).show();
            return;
        }
        ArrayList<Pair<String, String>> pairs = new ArrayList<>();
        pairs.add(new Pair<>("页面", "首页"));
        pairs.add(new Pair<>("栏目", "精选"));
        pairs.add(new Pair<>("位置", "24小时"));
        ArrayList<Pair<String, String>> tempPair = new ArrayList<>();
        tempPair.addAll(pairs);
        tempPair.add(new Pair<>("行为类型", "点击"));

        if (activity != null) {
            GridsumWebDissector.getInstance().trackEvent(activity, "", temp.getNewsTitle(), "", 200, tempPair);
        } else if (fragment != null) {
            GridsumWebDissector.getInstance().trackEvent(fragment, "", temp.getNewsTitle(), "", 200, tempPair);
        }
        HashMap<String, String> map = new HashMap<>();
        for (Pair<String, String> p : pairs) {
            map.put(p.first, p.second);
        }
        ReadNewsHelper.getInstance(context).registerReadListener(this);
        SkipUtil.skipWithNewsItem(context, temp, map);
    }

    @Override
    public void OnSetReadSuccess(String newsId) {
        if (temp.getUrlNewsId().equalsIgnoreCase(newsId)) {
            refreshHolder();
            ReadNewsHelper.getInstance(context).unRegisterReadListener(this);
        }
    }

    /**
     * 刷新新闻是否已读状态
     */
    public void refreshHolder() {
        if (temp.getIsBigPic() == 1) {
            if (ReadNewsHelper.getInstance(context).isNewsRead(temp.getUrlNewsId())) {
                bigNewsTitle.setTextColor(context.getResources().getColor(R.color.news_read_title));
            } else {
                bigNewsTitle.setTextColor(context.getResources().getColor(R.color.colorBlack22));
            }
        } else {
            if (ReadNewsHelper.getInstance(context).isNewsRead(temp.getUrlNewsId())) {
                title.setTextColor(context.getResources().getColor(R.color.news_read_title));
            } else {
                title.setTextColor(context.getResources().getColor(R.color.colorBlack22));
            }
        }
    }
}
